﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Ccit.OA.Utils;
using Ccit.Utils;

namespace Ccit.OA.NewsCenter
{
    public partial class news_queryset_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                _hidTabId.Value = ( null != Request.QueryString["TABID"]) ? Request.QueryString["TABID"].ToString() : "";
               // _hidWhereClause.Value = Ccit.OA.Utils.SysInterface.GetPageDataRangeSQL("NEWS_A2", _hidTabId.Value);
                
                //页面来源
                _hidPageSource.Value = (null != Request.QueryString["PageSource"]) ? Request.QueryString["PageSource"].ToString() : "";
                _hidType.Value=(null!=Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0" ;
                _hidOper.Value = (null != Request.QueryString["Oper"]) ? Request.QueryString["Oper"].ToString() : "";
                //编辑快速查询条件主键id
                _hidClauseId.Value = (null != Request.QueryString["clauseId"]) ? Request.QueryString["clauseId"].ToString() : "";
                if(_hidOper.Value=="1"){
                    trNewsStatu.Visible = false;
                    trButton.Visible = false;
                }
                tvColumn.Attributes.Add("oncheck", "tree_oncheck(this)");
                btnSearch.Attributes["onclick"] = "javascript:document.getElementById('_hidClauseXML').value=getNewsXML(); return checkNum();";
            }

            initial();   //设置树的可选范围

            BindTree(); 

            if (_hidClauseId.Value != "")//编辑
            {
                doEdit(_hidClauseId.Value);
            }
        }

        private void initial()
        {
            ColumnBLL bll = new ColumnBLL();
            List<MsColumn> lstColPurview = bll.GetColListByPurview(_hidType.Value);   //窗口管理页面
            if (_hidPageSource.Value == "1")  //信息发布 
            {
                lstColPurview = bll.GetColPub(_hidType.Value);
            }
            if (_hidPageSource.Value == "2")  //信息查询
            {
                lstColPurview = bll.GetColListByPurview(_hidType.Value);
            }
            if (_hidPageSource.Value == "3")  //信息管理
            {
                lstColPurview = bll.GetColManage(_hidType.Value);
                trNewsStatu.Style["display"] = "none";
            }

            string sRange = string.Empty;
            foreach (MsColumn col in lstColPurview)
            {
                sRange += (sRange == "") ? "'"+col.columnId+"'" : ",'" + col.columnId+"'";
            }
            _hidRangeColIds.Value = sRange;
        }

        #region   绑定树 
        public void BindTree()
        {
            string sRange = _hidRangeColIds.Value;
            tvColumn.Nodes.Clear();
            Microsoft.Web.UI.WebControls.TreeNode oSumNode = new Microsoft.Web.UI.WebControls.TreeNode();
            oSumNode.Text = "栏目管理";
            oSumNode.ID = "0";
            oSumNode.CheckBox = true;
            tvColumn.Nodes.Add(oSumNode);
            ColumnBLL bll = new ColumnBLL();

            List<MsColumn> list = bll.GetTreeData(_hidType.Value);
            foreach (MsColumn ms in list)
            {
                if (ms.columnLevelCode.Length == 4)
                {
                    Microsoft.Web.UI.WebControls.TreeNode oroot = new Microsoft.Web.UI.WebControls.TreeNode();
                    
                    if(sRange.Contains(ms.columnId))
                    {
                        oroot.CheckBox = true;
                    }
                    else
                    {
                        oroot.DefaultStyle["color"] = "red";
                    }

                    oroot.Text = ms.columnName;
                    oroot.ID = ms.columnId;
                    oSumNode.Nodes.Add(oroot);
                    string hasChild = (bll.isHasChildType(ms.columnLevelCode)) ? "1" : "0";
                    if (hasChild == "1")
                    {
                        BindSon(oroot, ms.columnId);
                    }
                }
            }
            tvColumn.ExpandLevel = 1;
        }
        private void BindSon(Microsoft.Web.UI.WebControls.TreeNode onode, string columnId)
        {
            ColumnBLL bll = new ColumnBLL();
            string sRange = _hidRangeColIds.Value;
            List<MsColumn> sonList = bll.GetSubColumnTypes(columnId);
            bool bChildHave = false;
            MsColumn fatherObj = bll.GetMsColumnObj(columnId);
            foreach (MsColumn mm in sonList)
            {
                if (mm.columnLevelCode.Length == fatherObj.columnLevelCode.Length + 4)
                {
                    Microsoft.Web.UI.WebControls.TreeNode oleaf = new Microsoft.Web.UI.WebControls.TreeNode();

                    if(sRange.Contains("'"+mm.columnId+"'"))
                    {
                        oleaf.CheckBox = true;
                    }
                    else
                    {
                        oleaf.DefaultStyle["color"] = "red";
                    }
                    oleaf.Text = mm.columnName;
                    oleaf.ID = mm.columnId;                    
                    onode.Nodes.Add(oleaf);
                    string hasChild = (bll.isHasChildType(mm.columnLevelCode)) ? "1" : "0";
                    if (hasChild == "1")
                    {
                        BindSon(oleaf, mm.columnId);
                    }
                }
            }
        }
        #endregion

        #region 私有方法
        //编辑
        private void doEdit(string clauseId)
        {
            QueryBLL bll = new QueryBLL();
            MsQueryClause mqc = bll.GetClauseObj(clauseId);
            MsClauseAttr mcAttr = new MsClauseAttr(mqc.ClauseContent);
            txtNewsTitle.Text = mcAttr.Title;
            txtPubPerson.Text = SysInterface.GetNameStrByIdStr(mcAttr.Creator, "0", " ");
            ddlNavigationImg.SelectedValue = mcAttr.PicFlag;
            ddlTopSign.SelectedValue = mcAttr.TopFlag;
            ddlNewsStatus.SelectedValue = mcAttr.NewsStatus;
            
            //栏目范围
            string [] ColumnIds=mcAttr.Category.Split(',');
            for (int i = 0; i < ColumnIds.Length;i++ )
            {
                SetCheckNode(ColumnIds[i]);
            }
            //时间范围
            if (mcAttr.DateType == "0")
                rdAllTime.Checked = true;
            if (mcAttr.DateType == "1")
                rdNowDay.Checked = true;
            if (mcAttr.DateType == "2")
                rdNowWeek.Checked = true;
            if (mcAttr.DateType == "3")
                rdNowMonth.Checked = true;
            if (mcAttr.DateType == "4")
            {
                rdBefore.Checked = true;
                txtBeforeDay.Text = mcAttr.DiffDays;
                txtBeforeDay.ReadOnly = false;
            }
            if (mcAttr.DateType == "5")
            {
                rdRange.Checked = true;
                txtBeginTime.Text = mcAttr.BeginDate;
                txtEndTime.Text = mcAttr.EndDate;
            }

            txtClauseName.Text = mqc.ClauseName;//条件名称
            WindowBLL winbll = new WindowBLL();
            if (winbll.GetWinStyleObj(mqc.ClauseId,_hidType.Value) != null)
                cbkAddNews.Checked = true;
        }
        //栏目范围的复选框选中
        private void SetCheckNode(string columnId)
        {
            ShowAllNodes(tvColumn.Nodes,columnId);
        }
        private void ShowAllNodes(Microsoft.Web.UI.WebControls.TreeNodeCollection nodes, string columnId)
        {
            foreach (Microsoft.Web.UI.WebControls.TreeNode node in nodes)
            {
                if(node.ID==columnId){
                    if (node.CheckBox == true)
                    {
                        node.Checked = true;
                    }
                    else
                    {
                        node.Text += "(权限已被撤销)"; 
                    }
                }
                if (node.Nodes.Count > 0)
                {
                    ShowAllNodes(node.Nodes,columnId);
                }
            }
        }

        #endregion



        #region Ajax Methods
        /// <summary>
        /// 保存快速查询
        /// </summary>
        /// <param name="jsonExpr"></param>
        /// <param name="addPlate"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string SaveFastClause(string jsonExpr, string addPlate,string Type,string PageSource)
        {
            MsQueryClause msc = (MsQueryClause)JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsQueryClause));
            QueryBLL bll = new QueryBLL();
            List<string> slist = bll.SaveFastClause(msc, addPlate,Type,PageSource);
            var oResult = new { resFlag = slist[0], resMes = slist[1], resColumnId = slist[2]};
            return "saveQuery" + "_" + JavaScriptConvert.SerializeObject(oResult);
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Session["QryClause"] = _hidClauseXML.Value;
            string pageSource=_hidPageSource.Value;
            string url = "";
            string sPageParam = "Type=" + _hidType.Value+"&TABID="+_hidTabId.Value;
            string encodePram = base64encode(sPageParam);
            if (pageSource=="1")   //信息发布
            {
                url = "news_submit_list.aspx?" + encodePram;
            }
            if (pageSource=="2")   //信息查询
            {
                url = "news_search_frame.aspx?" + encodePram;
            }
            if(pageSource=="3"){   //信息管理
                url = "news_list_manage.aspx?" + encodePram;
            }
            Response.Write("<script>parent.document.location='"+url+"'</script>");
        }

        private string base64encode(string sPageParam)
        {
            string encodeParam = string.Empty;
            if (string.IsNullOrEmpty(sPageParam)) return encodeParam;

            encodeParam = Base64Lib.Encode(sPageParam);
            return encodeParam;
        }
    }
}
